package com.service.admin.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.merchant.controller.rep.CreditLineStatementPageRep;
import com.service.admin.merchant.service.MerchantCreditLineStatementService;
import com.service.core.rest.request.PageRequest;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.mapper.MerchantCreditLineStatementMapper;
import com.service.dao.model.MerchantCreditLineStatement;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;


@Service("merchantCreditLineStatementService")
public class MerchantCreditLineStatementServiceImpl extends ServiceImpl<MerchantCreditLineStatementMapper, MerchantCreditLineStatement> implements MerchantCreditLineStatementService {

    @Override
    public CommonPageRep<CreditLineStatementPageRep> pageStatement(CoinsEnum coins, Integer creditLineId, PageRequest pageRequest) {

        Page<MerchantCreditLineStatement> page = PageHelper.startPage(pageRequest.getPageNo(), pageRequest.getPageSize());
        list(new QueryWrapper<MerchantCreditLineStatement>().lambda()
                .eq(MerchantCreditLineStatement::getCoinsId, coins.getCode())
                .eq(MerchantCreditLineStatement::getCreditLineId, creditLineId)
                .orderByDesc(MerchantCreditLineStatement::getCreateTime));

        return CommonPageRep.buildResultRep(page, page.getResult().stream()
                .map(item -> ModelMapperUtil.map(item, CreditLineStatementPageRep.class))
                .collect(Collectors.toList()));
    }

}
